package org.ohos.rajawali3d.examples;

import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

/**
 * @author Jared Woolston (jwoolston@keywcorp.com)
 */
public class PreferencesLocal implements Preferences.PreferencesObserver {

    private static final String WALLPAPER_RENDERER_KEY = "org.rajawali3d.examples.Preferences.wallpaper_renderer";
    public static final String PreferencesLocal_NAME = "org.rajawali3d.examples.Preferences";
    private static PreferencesLocal instance;
    private final Preferences preferences;
    private final DatabaseHelper dataBaseHelper;

    private String wallpaperRendererPreference;

    public static PreferencesLocal getInstance(Context context) {
        if (instance == null) {
            synchronized (PreferencesLocal.class) {
                if (instance == null) {
                    instance = new PreferencesLocal(context);
                }
            }
        }
        return instance;
    }

    private PreferencesLocal(Context context) {
        dataBaseHelper = new DatabaseHelper(context);
        preferences = dataBaseHelper.getPreferences(PreferencesLocal_NAME);
        preferences.registerObserver(this);

        updatePreferences();
    }

    private void updatePreferences() {
        wallpaperRendererPreference = preferences.getString(WALLPAPER_RENDERER_KEY, "");
    }

    public String getWallpaperRendererPreference() {
        return wallpaperRendererPreference;
    }

    @Override
    public void onChange(Preferences preferences, String s) {
        updatePreferences();
    }
}
